package dk.hewison.client.album;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.WindowResizeListener;
import com.google.gwt.user.client.ui.DockPanel;
import dk.hewison.client.domain.Directory;
import dk.hewison.client.panels.CompositeView;

/**
 * @author John Hewison
 * @author $LastChangedBy: john.hewison $:  $ $LastChangedDate: 2009-02-09 12:39:39 +0000 (Mon, 09 Feb 2009) $:
 * @version $Revision: 363 $:
 */
public class AlbumPageView extends CompositeView<Directory> implements WindowResizeListener {

    private DirectoryPathView directoryPathView;
    private AlbumSheet albumSheet;
    private PhotoSheet photoSheet;
    private DockPanel dockPanel;


    public void initView() {

        dockPanel = new DockPanel();

        directoryPathView = new DirectoryPathView();
        directoryPathView.initView();

        albumSheet = new AlbumSheet();
        albumSheet.initView();

        photoSheet = new PhotoSheet();
        photoSheet.initView();

        dockPanel.add(directoryPathView.getMyWidget(), DockPanel.NORTH);
        dockPanel.add(albumSheet, DockPanel.NORTH);
        dockPanel.add(photoSheet, DockPanel.CENTER);

        dockPanel.setWidth("100%");

        initWidget(dockPanel);

        DeferredCommand.addCommand(new Command() {
            public void execute() {
                onWindowResized(Window.getClientWidth(), Window.getClientHeight());
            }
        });

        Window.addWindowResizeListener(this);
    }

    public Directory getModel() {
        return null;
    }

    public void setModel(Directory directory) {
        directoryPathView.setModel(directory);
        albumSheet.setModel(directory.getSubDirectories());
        setAlbumSheetVisible(!directory.getSubDirectories().isEmpty());
        photoSheet.setModel(directory.getPhotos());
        onWindowResized(0, 0);
    }

    /**
     * Shows or hides the AlbumSheet.
     *
     * @param show should it be visible.
     */
    private void setAlbumSheetVisible(boolean show) {
        if (show == albumSheet.isAttached()) return;

        if (show) {
            dockPanel.add(albumSheet, DockPanel.NORTH);
        } else {
            dockPanel.remove(albumSheet);
        }
        onWindowResized(0, 0);
    }

    public void onWindowResized(int width, int height) {
        if (albumSheet.isAttached()) {
            albumSheet.resize();
        }
        photoSheet.resize();
    }
}
